package View.GUI;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.event.MouseInputListener;



import Controller.Game.GamePanelController;
import Model.Visible.Fleet;
import Model.Visible.Planet;
import Utils.ConstantsImplement;
import Utils.Utils;
import Utils.Timers.Timer;
import Utils.Timers.TimerListener;
import View.Game.GamePanel;


public class PlanetGUI implements MouseInputListener, MouseMotionListener, ConstantsImplement/*, TimerListener*/{


	// Constant Stuff

	private BufferedImage image_;
	private BufferedImage image_u;
	private BufferedImage voltaNyan_;
	private BufferedImage voltaNayn_;
	private Planet planet_;
	private boolean up;
	private boolean litlePanelUp;
	private GamePanelController gamePanelController_;
	private LittlePanelPlanetInfo littlePanelPlanetInfo;
	private ActionPlanetPanel actionPlanetPanel_;
	private AttackPanel attackPanel_;
	//private Timer timer_;
	private boolean actionPlanetPanelUp;
	private Timer attackTimer;
	private Fleet fleet;

	public PlanetGUI(Planet planet, GamePanelController gamePanelController) {
		// TODO Auto-generated constructor stub
		planet_ = planet;
		gamePanelController_ = gamePanelController;
		image_ = gamePanelController.getMedia_().image_;
		image_u = gamePanelController_.getMedia_().image_u;
		voltaNayn_ = gamePanelController_.getMedia_().voltaNayn_;
		voltaNyan_ = gamePanelController_.getMedia_().voltaNyan_;
		littlePanelPlanetInfo = new LittlePanelPlanetInfo(0, 0, this);
		actionPlanetPanel_ = new ActionPlanetPanel(planet_, this, gamePanelController_);
		litlePanelUp = false;
		actionPlanetPanelUp = false;
		attackPanel_ = new AttackPanel(gamePanelController,planet_);
		fleet= new Fleet(planet_.getOwner(), gamePanelController_);
		attackTimer = new Timer(new TimerListener() {

			@Override
			public void update() {
				// TODO Auto-generated method stub
				PlanetGUI.this.gamePanelController_.setCallToAttack(true);
				PlanetGUI.this.gamePanelController_.setWaitToAttack(true);
				while (PlanetGUI.this.gamePanelController_.isWaitToAttack()) {
					Thread.yield();
				}
				PlanetGUI.this.gamePanelController_.getGamePanel_().add(attackPanel_);
				//PlanetGUI.this.gamePanelController_.getGamePanel_().repaint();
				PlanetGUI.this.attackPanel_.setReady_(false);
				while (!(PlanetGUI.this.attackPanel_.isReady_())) {
					Thread.yield();
				}
				PlanetGUI.this.gamePanelController_.getClient_().sendMessage("ATK "+ PlanetGUI.this.planet_.hashCode() + " " + PlanetGUI.this.gamePanelController_.getAttaker_().getPlanet_().hashCode()+ " " + PlanetGUI.this.attackPanel_.getNumberOfShips_() );
				PlanetGUI.this.gamePanelController_.getGamePanel_().remove(attackPanel_);
				//System.out.println(gamePanelController_.getToAtack_());
				//System.out.println(PlanetGUI.this.attackPanel_.getNumberOfShips_());
				if(PlanetGUI.this.attackPanel_.getNumberOfShips_() > 0 && 
						PlanetGUI.this.attackPanel_.getNumberOfShips_() <= PlanetGUI.this.planet_.getFleet_().getNumShips()){
					PlanetGUI.this.fleet = PlanetGUI.this.planet_.getFleet_().divideFleet(
							PlanetGUI.this.attackPanel_.getNumberOfShips_(),
							PlanetGUI.this.planet_);
					FleetGUI fleetGui = new FleetGUI(fleet, gamePanelController_);
					fleet.setFleetGUI_(fleetGui);
					PlanetGUI.this.fleet.setPlanetToAttack(PlanetGUI.this.gamePanelController_.getAttaker_().getPlanet_());
					PlanetGUI.this.gamePanelController_.addFleetGUI(fleetGui);
				//System.out.println(fleet);
				PlanetGUI.this.gamePanelController_.getUniverse_().addFleet(PlanetGUI.this.fleet);
				}
				PlanetGUI.this.attackTimer.setRun_(false);
			}
		}, 100);
		attackTimer.setRun_(false);
		attackTimer.start();
		//timer_ = new Timer(this, 100);
		//timer_.start();
	}

	public GamePanelController getGamePanelController_() {
		return gamePanelController_;
	}

	public void setGamePanelController_(GamePanelController gamePanelController_) {
		this.gamePanelController_ = gamePanelController_;
	}

	public ActionPlanetPanel getActionPlanetPanel_() {
		return actionPlanetPanel_;
	}

	public void setActionPlanetPanel_(ActionPlanetPanel actionPlanetPanel_) {
		this.actionPlanetPanel_ = actionPlanetPanel_;
	}

	public void draw(Graphics g){
		if(planet_.getOwner() != null){
			if (planet_.getOwner().getRace_() == 0){
				g.drawImage(voltaNyan_, planet_.getPosition_().x - 10, planet_.getPosition_().y -10,70,70,
						null);
				
			}
			if (planet_.getOwner().getRace_() == 1){
				g.drawImage(voltaNayn_, planet_.getPosition_().x - 10, planet_.getPosition_().y -10,70,70,
						null);
				
			}
			
		}
		if (up == false){
			g.drawImage(image_, planet_.getPosition_().x, planet_.getPosition_().y,
					PLANET_SIZE, PLANET_SIZE, null);
		}
		else {
			g.drawImage(image_u, planet_.getPosition_().x, planet_.getPosition_().y,
					PLANET_SIZE, PLANET_SIZE,  null);
		}
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if( (gamePanelController_.getHostPlayer() == this.planet_.getOwner()) && arg0.getX() < PLANET_SIZE + planet_.getPosition_().getX() && arg0.getX() > planet_.getPosition_().getX() &&
				arg0.getY() < PLANET_SIZE + planet_.getPosition_().getY() && arg0.getY() > planet_.getPosition_().getY() && !(gamePanelController_.isCallToAttack())){
			if(arg0.getButton() == MouseEvent.BUTTON1){
				gamePanelController_.getGamePanel_().add(actionPlanetPanel_);
				//gamePanelController_.getGamePanel_().repaint();
				actionPlanetPanelUp = true;
			}
			if(arg0.getButton() == MouseEvent.BUTTON3){
				//System.out.println("Botao Direito");
				attackTimer.setRun_(true);
			}
		}else if (arg0.getX() < PLANET_SIZE + planet_.getPosition_().getX() && arg0.getX() > planet_.getPosition_().getX() &&
				arg0.getY() < PLANET_SIZE + planet_.getPosition_().getY() && arg0.getY() > planet_.getPosition_().getY() && (gamePanelController_.isCallToAttack())) {
			gamePanelController_.setToAtack_(this);
			gamePanelController_.setAttaker_(this);
			gamePanelController_.setWaitToAttack(false);
			gamePanelController_.setCallToAttack(false);
		}
		//System.out.println();
	}

	public boolean isActionPlanetPanelUp() {
		return actionPlanetPanelUp;
	}

	public void setActionPlanetPanelUp(boolean actionPlanetPanelUp) {
		this.actionPlanetPanelUp = actionPlanetPanelUp;
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub


	}

	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if( arg0.getX() < PLANET_SIZE + planet_.getPosition_().getX() && arg0.getX() > planet_.getPosition_().getX() &&
				arg0.getY() < PLANET_SIZE + planet_.getPosition_().getY() && arg0.getY() > planet_.getPosition_().getY()){
			up = true;
			if(!(litlePanelUp)){
				littlePanelPlanetInfo.setLocation(arg0.getX(), arg0.getY());
				gamePanelController_.getGamePanel_().add(littlePanelPlanetInfo);
				litlePanelUp = true;
				//gamePanelController_.getGamePanel_().repaint();
			}
		}
		else{
			up = false;
			if(litlePanelUp && !(isInLittle(arg0))){
				gamePanelController_.getGamePanel_().remove(this.littlePanelPlanetInfo);
				litlePanelUp = false;
				//gamePanelController_.getGamePanel_().repaint();
			}
		}
		gamePanelController_.getGamePanel_().setLabelText(String.valueOf(arg0.getX()) + "  " + String.valueOf(arg0.getY()));
	}


	public boolean isInLittle(MouseEvent arg0){
		if (arg0.getX() < littlePanelPlanetInfo.getX() + littlePanelPlanetInfo.getWidth() && arg0.getX() > littlePanelPlanetInfo.getX() &&
				arg0.getY() < littlePanelPlanetInfo.getY() + littlePanelPlanetInfo.getHeight() && arg0.getY() > littlePanelPlanetInfo.getY()){
			return true;
		}
		else {
			return false;
		}
	}

	public Planet getPlanet_() {
		return planet_;
	}

	public void setPlanet_(Planet planet_) {
		this.planet_ = planet_;
	}

//	public void update() {
//		// TODO Auto-generated method stub
//		//if (litlePanelUp || actionPlanetPanelUp)
//			//gamePanelController_.getGamePanel_().repaint();
//	}
}
